﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using Delay;
using Microsoft.Phone.Shell;
using PhoneInfo;
namespace PhoneInfo.Page
{
    public partial class Page1 : PhoneApplicationPage
    {
        private string topicid=null;
        ReplyResult replyResult;
        public bool intoChange=true;
        private InfoType pageType;
        public Page1()
        {
            InitializeComponent();
            if (App.appSitting.Theme == 1)
            {
                ApplicationBar = (ApplicationBar)this.Resources["AppBar"];
            }
            else
            {
                ApplicationBar = (ApplicationBar)this.Resources["AppBar1"];
            }
            ReplyContent.Focus();
            Loaded += (_, __) => { ReplyContent.Focus(); };
        }
        private void Reply(object sender, EventArgs e)
        {
            string content=ReplyContent.Text;
            if (content.Length==0)
            {
               MessageBox.Show("评论的内容不能为空");
               return;
            }
            if (replyResult==null)
            {
                replyResult = new ReplyResult();
                replyResult.Result = -1;
                replyResult.ResultMsg = "";
                replyResult.IsLogo = -1;
                replyResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(replyResult_PropertyChanged);
                replyResult.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(replyResult.GetPhoneInfoUrl(topicid, content,pageType,App.lxt)));
                //此时才发表评论
                FullModePopup.IsOpen = true;
                rezultMsg.Visibility = Visibility.Collapsed;
                ProgBar.Visibility = Visibility.Visible;
                btnsubmit.Focus();
                ApplicationBar = null;
            }
        }
        void replyResult_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //throw new NotImplementedException();
            if (sender==null)
            {
                return;
            }
            ReplyResult se = (ReplyResult)sender;
            if (e.PropertyName == "Result")
            {
                //进行处理
                if (se.Result == -1 || se.Result == 1)
                {
                    rezultMsg.Text = se.ResultMsg;
                    rezultMsg.Visibility = Visibility.Visible;
                    ProgBar.Visibility = Visibility.Collapsed;
                    ApplicationBar = ((ApplicationBar)this.Resources["AppBar"]);
                }
                else if (se.Result == 2)
                {//success
                    rezultMsg.Text = se.ResultMsg;
                    rezultMsg.Visibility = Visibility.Visible;
                    ProgBar.Visibility = Visibility.Collapsed;
                    ApplicationBar = ((ApplicationBar)this.Resources["AppBar"]);
                    FullModePopup.IsOpen = false;
                    MessageBox.Show("发表成功");
                    NavigationService.GoBack();
                }
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重写基类Page的虚方法，当页面变为框架的活动页面时调用
        {
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;
            if (parameters.ContainsKey("topicid"))
            {
                topicid = parameters["topicid"];
            }
            if (parameters.ContainsKey("ShrewdEye"))
            {
                pageType = InfoType.Eye;//精明眼

            }
            else
            {
                pageType = InfoType.Info;//资讯列表
            }
            
        }

        private void cancl(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}